﻿using System;
using Commands;
using ShapeManager;
using UserControls;
using System.Windows.Forms;

namespace ROI编辑器
{
    public partial class MainForm : Form
    {
        UImageWindow uImageWindow;
        public MainForm()
        {
            InitializeComponent();
            this.CenterToParent();
            CheckForIllegalCrossThreadCalls = true;
        }
        #region 事件
        private void MainForm_Load(object sender, EventArgs e)
        {
            uImageWindow = new UImageWindow();
            uImageWindow.Dock = DockStyle.Fill;
            uImageWindow.Owner = this;
            panel_Container.Controls.Add(uImageWindow);
            uImageWindow.Initialize(this);
            Application.Idle += delegate (object o, EventArgs a)
            {
                SetStateOfControls();
            };
        }
        private void toolsBtn_Undo_Click(object sender, EventArgs e)
        {
            CommandUndo();
        }
        private void toolsBtn_Redo_Click(object sender, EventArgs e)
        {
            CommandRedo();
        }
        private void toolsBtn_Delete_Click(object sender, EventArgs e)
        {

            ICommand command = new CommandDelete(uImageWindow.ShapeList);
            if (uImageWindow.ShapeList.DeleteSelection())
            {
                uImageWindow.SetDirty();
                uImageWindow.Refresh();
                uImageWindow.AddCommandToHistory(command);
            }
        }
        private void toolsBtn_Clear_Click(object sender, EventArgs e)
        {
            ICommand command = new CommandDeleteAll(uImageWindow.ShapeList);
            if (uImageWindow.ShapeList.Clear())
            {
                uImageWindow.SetDirty();
                uImageWindow.Refresh();
                uImageWindow.AddCommandToHistory(command);
            }
        }
        private void toolsBtn_Cursor_Click(object sender, EventArgs e)
        {
            uImageWindow.ActiveTool = ShapeType.Pointer;
        }
        private void toolsBtn_Rect_Click(object sender, EventArgs e)
        {
            uImageWindow.ActiveTool = ShapeType.Rectangle;
        }
        private void toolsBtn_RectR_Click(object sender, EventArgs e)
        {
            uImageWindow.ActiveTool = ShapeType.RectangleR;
        }
        private void toolsBtn_Ellipse_Click(object sender, EventArgs e)
        {
            uImageWindow.ActiveTool = ShapeType.Ellipse;
        }
        private void toolsBtn_Circle_Click(object sender, EventArgs e)
        {
            uImageWindow.ActiveTool = ShapeType.Circle;
        }
        private void toolsBtn_Polygon_Click(object sender, EventArgs e)
        {
            uImageWindow.ActiveTool = ShapeType.Polygon;
        }
        #endregion

        #region 方法
        public void SetStateOfControls()
        {
            bool objects = (uImageWindow.ShapeList.Count > 0);
            bool selectedObjects = (uImageWindow.ShapeList.SelectionCount > 0);
            toolsBtn_Undo.Enabled = uImageWindow.CanUndo;
            toolsBtn_Redo.Enabled = uImageWindow.CanRedo;
            toolsBtn_Delete.Enabled = selectedObjects;
            toolsBtn_Clear.Enabled = objects;
        }
        private void CommandUndo()
        {
            uImageWindow.Undo();
        }
        private void CommandRedo()
        {
            uImageWindow.Redo();
        }
        #endregion

        
    }
}
